﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Diagnostics;
using Facebook.Service;

namespace FriendsNearby.Services.Tests
{
    [TestFixture]
    [Ignore("Manual Test Fixture for Facebook Session Keys")]
    public class FacebookSessionsFixture
    {
        FriendsNearbyFacade fnFacade;
        List<string> registeredUserIds;

        [SetUp]
        public void SetUp()
        {
            Database database = DatabaseFactory.CreateDatabase("FriendsNearbyDB");
            FriendsDataDataContext ctx = new FriendsDataDataContext(database.CreateConnection());            
			var query = from registry in ctx.UserRegistries
						select registry.FacebookUserId;
            registeredUserIds = new List<string>(query);
            fnFacade = new FriendsNearbyFacade();            
        }

        [Test]
        public void ShouldGetUserNamesOfRegisteredUsers()
        {
            Debug.WriteLine("ShouldGetUserNamesOfRegisteredUsers");
            Debug.WriteLine("uid\t\t\tfs.uid\t\texpires?\tname");
            foreach (string userId in registeredUserIds)
            {
                var fbService = fnFacade.GetService<IFacebookServiceFactory>().CreateFacebookService(userId);
                Debug.Write(userId);
                Debug.Write("\t");
                string _userId = fbService.UserID;
                Debug.Write(_userId);
                Debug.Write("\t");
                Debug.Write(fbService.Session.SessionExpires);
                Debug.Write("\t\t");
                string name = TryGetName(fbService, userId);
                Debug.Write(name);
                Debug.WriteLine(string.Empty);
            }
        }

        [Test]
        public void ShouldGetFriendsWithAppOfRegisteredUsers()
        {
            Debug.WriteLine("ShouldGetFriendsWithAppOfRegisteredUsers");
            Debug.WriteLine("uid\t\t\tfs.uid\t\texpires?\t#friends");
            foreach (string userId in registeredUserIds)
            {
                var fbService = fnFacade.GetService<IFacebookServiceFactory>().CreateFacebookService(userId);
                Debug.Write(userId);
                Debug.Write("\t");
                string _userId = fbService.UserID;
                Debug.Write(_userId);
                Debug.Write("\t");
                Debug.Write(fbService.Session.SessionExpires);
                Debug.Write("\t\t");
                string name = TryGetFriendsCount(fbService);
                Debug.Write(name);
                Debug.WriteLine(string.Empty);
            }
        }

        private string TryGetFriendsCount(FacebookService fs)
        {
            try
            {
                return fs.Friends.GetFriends(FriendFilter.AppUsers).Count.ToString();
            }
            catch (Exception e)
            {
                return string.Format("<Error:{0}>", e.Message);
            }
        }

        public string TryGetName(FacebookService fs, string userId)
        {
            try
            {
                return fs.Users.GetUser(userId, null).Name;
            }
            catch (Exception e)
            {
                return string.Format("<Error:{0}>", e.Message);
            }
        }

    }
}
